package com.caiheng.api.service.survey.impl;

import com.caiheng.api.dao.survey.SurveyMapper;
import com.caiheng.api.dao.survey.SurveyQuestionMapper;
import com.caiheng.api.entity.survey.Survey;
import com.caiheng.api.entity.survey.SurveyQuestion;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.survey.SurveyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问卷调查Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Service
public class SurveyServiceImpl extends BaseServiceImpl<SurveyMapper, Survey> implements SurveyService {

    @Resource
    SurveyMapper surveyMapper;
    @Resource
    SurveyQuestionMapper surveyQuestionMapper;

    @Override
    @Transactional
    public int addSurvey(int userId, Survey survey) {
        int ret = 0;
        try{
            List<SurveyQuestion> ques = surveyQuestionMapper.selectListByColumn("survey_id",survey.getId());
            for (SurveyQuestion question: ques){
                ret=  surveyQuestionMapper.deleteById(question.getId());
                if(ret <= 0) {
                    throw new TransactionalException("surveyQuestionMapper insert error");
                }
            }

            ret = surveyMapper.deleteById(survey.getId());
            if(ret <= 0){
                throw new TransactionalException("surveyMapper insert error");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }
}
